package com.wdemo.config;

import com.wdemo.memory.RedisChatMemory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPooled;

/**
 * @ClassName SpringAiController
 * @Description springAi配置类
 * @Author WDEMO
 * @Date 2025/8/29 12:05
 */
@Slf4j
@Configuration
public class SpringAiConfig {


    @Autowired
    private AIProperties aiProperties;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Bean
    public Advisor simpleLoggerAdvisor(){
        return new SimpleLoggerAdvisor();
    }


    @Bean
    public ChatMemory redisChatMemory(){
        return new RedisChatMemory(stringRedisTemplate);
    }

    @Bean
    public MessageChatMemoryAdvisor messageChatMemoryAdvisor(ChatMemory redisChatMemory){
        return MessageChatMemoryAdvisor.builder(redisChatMemory).build();
    }

    @Bean
    public JedisPooled jedisPooled() {
        return new JedisPooled("192.168.100.128", 6378);
    }
    @Bean
    public VectorStore vectorStore(JedisPooled jedisPooled, EmbeddingModel embeddingModel){
        RedisVectorStore redisVectorStore = RedisVectorStore.builder(jedisPooled, embeddingModel)
                .prefix("EMBEDDING:OTHER")
                .indexName("OTHER_INDEX")
                .initializeSchema(true)
                .build();
        return redisVectorStore;
    }

    @Bean
    public ChatClient  chatClient(ChatModel chatModel,
                                 Advisor simpleLoggerAdvisor,
                                 MessageChatMemoryAdvisor messageChatMemoryAdvisor){
        return ChatClient.builder(chatModel)
                .defaultAdvisors(simpleLoggerAdvisor,messageChatMemoryAdvisor)
                .defaultSystem(aiProperties.getSystemChatMessage().get())
                .build();
    }
}
